﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "GnufDisplayAndPickFilterBoard.h"
#include "GnufExplorer.h"
#include "GnufModelViewBoard.h"
#include "GnufArrangementArea.h"
#include "GnufArrangementAreaContent.h"
#include "GnufArrangementAreaEvents.h"
#include "GnufInfo.h"
#include "GnufPreferences.h"
#include "GnufQtTree.h"
#include "GnufUtility.h"
#include "QtGnufExplorer.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gfam;

namespace 
{
    static const std::wstring s_key(L"新资源管理器");
    static GnufExplorer s_GnufExplorer;
}

GnufExplorer * GnufExplorer::Get()
{
    if (!gnuf::ArrangementAreaContentManager::get())
    {
        return nullptr;
    }

    return static_cast<GnufExplorer*>(gnuf::ArrangementAreaContentManager::get()->contentBy(s_key));
}

GnufExplorer::GnufExplorer()
    : gnuf::ArrangementAreaContent(s_key)
{}

GnufExplorer::~GnufExplorer()
{}

QWidget * GnufExplorer::qtWidget()
{
    if (nullptr != m_pallet && nullptr != m_pallet->qtWidget()) 
    {
        return m_pallet->qtWidget();
    }

    return nullptr;
}

gnuf::TreeBoard * gfam::GnufExplorer::GetTreeBoard()
{
    if (!m_qtGnufExplorer)
    {
        return nullptr;
    }

    return m_qtGnufExplorer->modelViewTree();
}

void GnufExplorer::onPostGeometryChanged()
{
    gnuf::Utility::refreshQtWidgetGeometry(this);
}

void GnufExplorer::onPostVisibilityChanged()
{
    if (nullptr != qtWidget() && nullptr != arrangementArea())
   {
        qtWidget()->setVisible(arrangementArea()->visible());
    }
}

void GnufExplorer::onPostOpacityChanged()
{
    if (nullptr != qtWidget() && nullptr != arrangementArea())
    {
        qtWidget()->setWindowOpacity(arrangementArea()->opacity());
    }
}

void GnufExplorer::onPostConnectedWithArrangementArea()
{
    int currentTab = 0;

    m_qtGnufExplorer = TransferOwnership(NEW_AS_OWNER_PTR(QtGnufExplorer, this));
    if (m_qtGnufExplorer)
    {
        if (modelViewBoard())
        {
            modelViewBoard()->Initialize(m_qtGnufExplorer->modelViewTree());
        }

        if (displayFilterBoard())
        {
            displayFilterBoard()->Initialize(m_qtGnufExplorer->displayFilterTree(), m_qtGnufExplorer->displayFilterRule());
        }

        m_qtGnufExplorer->setCurrentTab(currentTab);
    }
    
    if (m_pallet)
    {
        // Embed the dialog into the pallet.
        m_pallet = NEW_AS_OWNER_PTR(gnuf::Pallet, arrangementArea(), true);
        m_pallet->setEventHander(this);
        m_pallet->rHeader().addTab(NEW_AS_OWNER_PTR(
            gnuf::Info, GBMP_TR(L"文档管理"), GBMP_TR(L"文档管理"), L":image/ProjectManagement/"));
        m_pallet->rHeader().addTab(NEW_AS_OWNER_PTR(
            gnuf::Info, GBMP_TR(L"显示过滤"), GBMP_TR(L"显示过滤"), L":image/DisplayFilter/"));
        m_pallet->rHeader().wantBorder.bottom = true;
        m_pallet->rHeader().showOnlyTabIcon = true;
        m_pallet->rHeader().padding.left = 0;
        m_pallet->rHeader().padding.top = 0;
        m_pallet->rHeader().padding.right = 0;
        m_pallet->rHeader().padding.bottom = 0;
        m_pallet->rHeader().padding.spacing = 0;
        m_pallet->rHeader().currentTab = currentTab;
        m_pallet->setQtContent(m_qtGnufExplorer.get());
        m_pallet->setWantBorder(true);
        m_pallet->show();
    }

    gnuf::Utility::setQtWidgetParent(this);
}

void GnufExplorer::onPreDisconnectedFromArrangementArea()
{
    if (m_qtGnufExplorer.ReleaseOwnership())
    {
        m_qtGnufExplorer.ReleaseOwnership()->destroy();
    }

    if (m_pallet)
    {
        m_pallet->setEventHander(nullptr);
        m_pallet->hide(true);
        m_pallet.reset(nullptr);
    }
}

void GnufExplorer::onHeaderTabChanged(gnuf::Pallet * pallet)
{
    //在current为displayfilterBoard时创建明细表等,再切回到modelviewBoard,新增的节点不会在面板中显示,需要刷新一下。
    if (!pallet)
    {
        return;
    }

    if (pallet && pallet->header().currentTab == 0)
    {
        if (modelViewBoard())
        {
            modelViewBoard()->Refresh();
        }
    }

    if (!m_qtGnufExplorer)
    {
        return;
    }

    m_qtGnufExplorer->setCurrentTab(pallet->header().currentTab);
}

GnufModelViewBoard* GnufExplorer::modelViewBoard()
{
    if (m_modelViewBoard.get() == nullptr)
    {
        m_modelViewBoard = NEW_AS_OWNER_PTR(GnufModelViewBoard);
    }

    return m_modelViewBoard.get();
}

GnufDisplayAndPickFilterBoard* GnufExplorer::displayFilterBoard()
{
    if (m_displayFilterBoard.get() == nullptr)
    {
        m_displayFilterBoard = NEW_AS_OWNER_PTR(GnufDisplayAndPickFilterBoard);
    }

    return m_displayFilterBoard.get();
}

void GnufExplorer::onEvent(gnuf::ArrangementAreaEvent* evt)
{
    switch (evt->type())
    {
    case gnuf::ArrangementAreaEvent::EType::ePreferencesChanged:
        auto theEvt = static_cast<gnuf::PreferencesChangedAreaEvent*>(evt);
        const gnuf::Preferences* preferences = theEvt->preferences();
        DBG_WARN_AND_RETURN_VOID_UNLESS(preferences, L"preferences is nullptr", L"paramodel", L"2024-02-20");

        if (const auto* entry = preferences->changedEntryBy(gnuf::PreferenceEntry::eTheme))
        {
            if (m_qtGnufExplorer && m_pallet)
            {
                m_qtGnufExplorer->refreshAppearance();
                m_pallet->refresh();
            }
        }break;
    }
}
